const numFilter = (value) => {
    let realVal = parseFloat(value).toFixed(2);
    return realVal;
}
const money=(s,type)=>{
    var f=false;
    if(s<0) {
        f=true;
        s=(s+'').replace('-','')
    }
    
    if (/[^0-9\.]/.test(s))
         return "0.00";
    if (s == null || s == "null" || s == "")
         return "0.00";
     s = s.toString().replace(/^(\d*)$/, "$1.");
     s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
     s = s.replace(".", ",");
     var re = /(\d)(\d{3},)/;
     while (re.test(s))
         s = s.replace(re, "$1,$2");
     s = s.replace(/,(\d\d)$/, ".$1");
    
     if (!type || type == 0) {
         var a = s.split(".");
         if (a[1] == "00") {
             s = a[0];
         }
     }
     if(f) {
         return '-'+s
     }
     if(type==2) {
         return parseFloat(s)
     }
     return s;
}
/**
 * 将后台api返回图片地址通过代理获取图片链接
 * @param value: 图片Url，形如'19;/uploaded/Images/xxxx.jpg',拆分获取图片id及服务器地址
 * @returns {拆分后加上proxy定义的转发代理地址请求获取图片的地址}
 */
 const showImg = (value) => {
    if (!value) return require('@/assets/images/default-img.jpg');
    const imguri = value.toString().split(";", 2).slice(1)
    return '/images' + imguri;
  }

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}
/**
 * 将时间按指定格式过滤输出  'yyyy-MM-dd hh:mm:ss'
 * @param date: Date/string 
 * @param fmt: 'yyyy-MM-dd hh:mm'
 * @returns {按指定格式输出 年 月 日 时 分 秒}
 */
const formatDate = (date, fmt) => {
    if(date === undefined) return '';
    if(typeof date === 'string') date = new Date(date.replace(/-/g, '/'))//new Date(date); //IOS 时间为NaN问题
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
}

const splitInfo = (value, seperator, index) => {
    if (value === null) return '';
    var infoArr = value.split(seperator||'-', 2);
    return (typeof infoArr[index] !== 'undefined' && infoArr[index] !== null) ? infoArr[index] : "";
}
 export {numFilter, money, showImg, formatDate, splitInfo}